package com.liull.netty.nio.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @Author: liull
 * @Description:
 * @Date: Created in 12:17 2021/7/25
 * @Modified By:
 */
public class LengthFieldBasedDecoderTest {
    public static void main(String[] args) {
        EmbeddedChannel channel = new EmbeddedChannel(
                new LengthFieldBasedFrameDecoder(65535, 0, 4, 0, 4),
                new LoggingHandler(LogLevel.DEBUG));
        send(channel, "Hello, world");
        send(channel, "Hi");
    }

    private static void send(EmbeddedChannel channel, String content) {
        final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        final byte[] bytes = content.getBytes();
        final int length = bytes.length;
        buf.writeInt(length);
        buf.writeBytes(bytes);
        channel.writeInbound(buf);
    }
}
